/**
 * 文件：InsertStuPanel.java
 * 说明：用于添加学生
 * 作者：古通基
 * 时间：1211-09-13
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class InsertStuPanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -4539380882233271063L;
	private JLabel lno = new JLabel("学号：");                                                //静态标签
	private JLabel lname = new JLabel("姓名：");
	private JLabel lsex = new JLabel("性别：");             
	private JLabel lnation = new JLabel("民族：");       
	private JLabel lcollege = new JLabel("院系：");       
	private JLabel lspecialty = new JLabel("专业：");     
	private JLabel lbirthday = new JLabel("生日：");     
	private JLabel lgrade = new JLabel("年级：");         

	private JTextField no = new JTextField("请输入五位学号",10);                    //文本输入
	private JTextField name = new JTextField("请输入学生姓名",10); 
	private JTextField birthday = new JTextField("yyyy-MM-dd",10); 
    
    String[] sexs = {"男","女"};                                                                         //选择列表
    private JComboBox sex = new JComboBox(sexs);    
    String[] nations = {"汉族","壮族","苗族","黎族","满族","瑶族","藏族","蒙族","其他"};
    private JComboBox nation = new JComboBox(nations);    
    String[] colleges = {"信息工程学院","外国语学院","食品工程学院","海洋生命学院","数学科学学院"};
    private JComboBox college = new JComboBox(colleges);    
    String[]  specialties= {"计算机","朝鲜语","营养科学","生物科学","应用数学"};
    private JComboBox specialty = new JComboBox(specialties);    
    String[]  grades= {"2008","2009","2010","2011"};
    private JComboBox grade = new JComboBox(grades);    
    
    private JButton insert = new JButton("添加");                                   //声明按钮
    private JButton reset = new JButton("重置");
    
    private JPanel JPcomon1 = new JPanel();                                         //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPcomon6 = new JPanel();
    private JPanel JPcomon7 = new JPanel();
    private JPanel JPcomon8 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public InsertStuPanel()                                                                    //构造函数
    {
    	lno.setHorizontalAlignment(SwingConstants.CENTER);
    	lname.setHorizontalAlignment(SwingConstants.CENTER);
    	lsex.setHorizontalAlignment(SwingConstants.CENTER);
    	lnation.setHorizontalAlignment(SwingConstants.CENTER);
    	lcollege.setHorizontalAlignment(SwingConstants.CENTER);
    	lspecialty.setHorizontalAlignment(SwingConstants.CENTER);
    	lbirthday.setHorizontalAlignment(SwingConstants.CENTER);
    	lgrade.setHorizontalAlignment(SwingConstants.CENTER);
   	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(lno);
    	JPcomon1.add(no);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(lname);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(lsex);
    	JPcomon3.add(sex);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(lnation);
    	JPcomon4.add(nation);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(lcollege);
    	JPcomon5.add(college);
    	JPcomon6.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon6.add(lspecialty);
    	JPcomon6.add(specialty);
    	JPcomon7.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon7.add(lbirthday);
    	JPcomon7.add(birthday);
    	JPcomon8.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon8.add(lgrade);
    	JPcomon8.add(grade);
    	
    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(insert);
    	JPbutton.add(reset);
    	
    	this.setLayout(new GridLayout(10,1));                                             //挂载面板
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPcomon6);
    	this.add(JPcomon7);
    	this.add(JPcomon8);
    	this.add(JPbutton);
    	
    	insert.addActionListener(this);                                                        //事件监听        
    	reset.addActionListener(this);
    	no.addMouseListener(this);
    	name.addMouseListener(this);
    	birthday.addMouseListener(this);
    }    
    
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("添加"))
		{
				try 
				{
					AddToDataBase();
				}
				catch (IllegalArgumentException e1) 
				{
					JOptionPane.showMessageDialog(this, e1, "系统错误",JOptionPane.INFORMATION_MESSAGE);
				}
		}
		else if(e.getActionCommand().equals("重置"))
		{
			Reset();
		}
	}
	
	private void AddToDataBase() throws IllegalArgumentException
	{
		try
		{
			BaseSQL stu = null;
			stu = new BaseSQL();
			String sql = "insert into student values('"+Integer.parseInt(no.getText())+"','"+name.getText()+"','"+(String) sex.getSelectedItem()+"','"+(String) nation.getSelectedItem()+"','"+(String) college.getSelectedItem()+"','"+(String) specialty.getSelectedItem()+"','"+java.sql.Date.valueOf(birthday.getText())+"','"+Integer.parseInt((String) grade.getSelectedItem())+"')";
		    stu.executeDML(sql);
		    JOptionPane.showMessageDialog(this, "已成功添加学号为："+Integer.parseInt(no.getText())+" 的学生", "添加学生",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}
	
	private void Reset()
	{
		no.setText("");
		name.setText("");
		birthday.setText("");
		grade.setSelectedIndex(3);
	}
	
    public void mouseClicked(MouseEvent e) { }                                //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入五位学号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}
    	else if(name.getText().equals("请输入学生姓名"))
    	{
    		name.setText("");
    		this.setVisible(true);
    	}
    	else if(birthday.getText().equals("yyyy-MM-dd"))
    	{
    		birthday.setText("");
    		this.setVisible(true);
    	}
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}
